package db;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import utils.Log4jInit;
import utils.UserLogUtil;


//@Deprecated
public class DBDruidManager {
	public static Logger logger = LoggerFactory.getLogger(DBDruidManager.class);
	public static final String JDBC_CONF_FILE = "jdbc.properties"; 
	

	private static ThreadLocal connectionThreadLocal = new ThreadLocal();
//	private static DruidDataSource druidDataSource = null;
	private static DruidDataSource druidDataSource;
	
	public static DruidDataSource getDruidDataSource() {
		return druidDataSource;
	}

	public static void setDruidDataSource(DruidDataSource druidDataSource) {
		DBDruidManager.druidDataSource = druidDataSource;
	}

	static {
		Properties properties = loadPropertiesFile(JDBC_CONF_FILE);
		try {
			druidDataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("[JDBC Exception] --> "
					+ "Failed to configured the Druid DataSource, the exceprion message is:" + e.getMessage());
			//Log4jInit.logger.info(e.getMessage());
		}
	}

	/*public static Connection getConnection() {
		Connection connection = (Connection) connectionThreadLocal.get();
		try {
			if (null == connection) {
				connection = druidDataSource.getConnection();
				connectionThreadLocal.set(connection);
			}
<<<<<<< HEAD
		} catch (SQLException e) {
			logger.error("[JDBC Exception] --> " + "Failed to create a connection, the exceprion message is:"
					+ e.getMessage());
		}
		return connection;
	}*/
	
	public static Connection getConnection() {
		Connection connection = (Connection) connectionThreadLocal.get();
		try {
		//	if (null == connection) {
				connection = druidDataSource.getConnection();
				connectionThreadLocal.set(connection);
	//		}
		} catch (SQLException e) {
			logger.error("[JDBC Exception] --> " + "Failed to create a connection, the exceprion message is:"
					+ e.getMessage());
			e.printStackTrace();
		}
		if(connection==null)
			Log4jInit.logger.info("[JDBC Exception] --> Failed to create a connection, the exceprion message is null");
		return connection;
	}

	public static void closeConnection() {
		Connection connection = (Connection) connectionThreadLocal.get();
		if (null != connection) {
			try {
				connection.close();
				connectionThreadLocal.remove();
			} catch (SQLException e) {
				logger.error("[JDBC Exception] --> "
						+ "Failed to close the DruidPooledConnection, the exceprion message is:" + e.getMessage());
				e.printStackTrace();
			}
		}
	}
	
	public static void closeConnection(Connection conn, Statement statement, PreparedStatement prepStatement,
			ResultSet rs) {
		Connection connection = (Connection) connectionThreadLocal.get();
		if (null != connection) {
			try {
				if (rs != null) {
					rs.close();
				}
				if (statement != null) {
					statement.close();
				}
				if (prepStatement != null) {
					prepStatement.close();
				}
				if(conn!=null){
					conn.close();
				}
				
//				connection.close();
				connectionThreadLocal.remove();
			} catch (SQLException e) {
				logger.error("[JDBC Exception] --> "
						+ "Failed to close the DruidPooledConnection, the exceprion message is:" + e.getMessage());
			}
		}
	}
	
//Transaction
	public static void startTransaction() {
		Connection conn = (Connection) connectionThreadLocal.get();

		try {
			if (conn == null) {
				conn = getConnection();
				connectionThreadLocal.set(conn);
			}
			conn.setAutoCommit(false);
		} catch (Exception e) {
			logger.error("[JDBC Exception] --> " + "Failed to start the transaction, the exceprion message is:"
					+ e.getMessage());
			e.printStackTrace();
		}
	}

	public static void commit() {
		try {
			Connection conn = (Connection) connectionThreadLocal.get();
			if (null != conn) {
				conn.commit();
			}
		} catch (Exception e) {
			logger.error("[JDBC Exception] --> " + "Failed to commit the transaction, the exceprion message is:"
					+ e.getMessage());
			e.printStackTrace();
		}
	}

	public static void rollback() {
		try {
			Connection conn = (Connection) connectionThreadLocal.get();
			if (conn != null) {
				conn.rollback();
				connectionThreadLocal.remove();
			}
		} catch (Exception e) {
			logger.error("[JDBC Exception] --> " + "Failed to rollback the transaction, the exceprion message is:"
					+ e.getMessage());
			e.printStackTrace();
		}
	}

	private static Properties loadPropertiesFile(String fullFile) {
		if (null == fullFile || fullFile.equals("")) {
			throw new IllegalArgumentException("Properties file path can not be null" + fullFile);
		}
		Properties prop = new Properties();
		try {
			prop.load(DBDruidManager.class.getClassLoader().getResourceAsStream(fullFile));
			UserLogUtil.log("jdbc url:"+prop.getProperty("url"));
		} catch (IOException e) {
			logger.error("[Properties Exception] --> " + "Can not load jdbc properties, the exceprion message is:"
					+ e.getMessage());
			e.printStackTrace();
		}
		return prop;
	}
}

